package com.library.lockConsumer.defender;

import com.library.lockProvider.service.BoxAndCabinetService;
import com.library.lockProvider.service.impl.BoxAndCabinetImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;



@Configuration
public class Listener implements ApplicationListener<ContextRefreshedEvent>, Runnable {

    @Autowired
    BoxAndCabinetService boxAndCabinetService;

    @Override
    public void run() {
        System.err.println("EUN");
        Date before=new Date();
        Date now=new Date();
        while(true) {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            now=new Date();
            long n=now.getTime();
            long b=before.getTime();
            if((n-b)/(1000 * 60 * 60 * 24)>=1) {
                System.err.println("更新记录");
                before=now;
                boxAndCabinetService.initAllBox();
            }
        }
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        Listener listener = contextRefreshedEvent.getApplicationContext().getBean(Listener.class);
        new Thread(this).start();
    }
}
